package com.yimi.yinhepda.views.unload_module;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yimi.yinhepda.R;
import com.pda.ym.base.BaseTitleActivity;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.pda.ym.utils.FastClick;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class UnloadSuccessActivity extends BaseTitleActivity {
    private EditText activity_unload_success_carcode, activity_unload_success_src,
            activity_unload_success_desc, activity_unload_success_carno, activity_unload_success_realweight,
            activity_unload_success_checkweight;

    private Button activity_unload_success_btn;

    private String task;
    private UnloadTaskTableDao mUnloadTaskTableDao;
    private double weight;
    private double chargedWeight;

    @Override
    public int setContentViewId() {
        return R.layout.activity_unload_success;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        task = getIntent().getStringExtra("task");
        weight = getIntent().getDoubleExtra("weight", 0);
        chargedWeight = getIntent().getDoubleExtra("chargedWeight", 0);

        mUnloadTaskTableDao = new UnloadTaskTableDao();
        List<UnloadTaskTable> list = mUnloadTaskTableDao.findData(task);
        String carMark = "";
        String srcDeptName = "";
        String destDeptName = "";
        String carNo = "";
        for (UnloadTaskTable item : list) {
            if (!item.getStowageNo().startsWith("P")) {
                carMark += item.getCarMark() + "/";
            } else {
                carMark += item.getStowageNo() + "/";
            }
            if (!srcDeptName.contains(item.getSrcDeptName())) {
                srcDeptName += item.getSrcDeptName() + "/";
            }
            if (!destDeptName.contains(item.getDestDeptName())) {
                destDeptName += item.getDestDeptName() + "/";
            }
            if (!carNo.contains(item.getCarNo())) {
                carNo += item.getCarNo() + "/";
            }
        }

        activity_unload_success_carcode.setText(carMark.substring(0, carMark.length() - 1));
        if (!TextUtils.isEmpty(srcDeptName)){
            activity_unload_success_src.setText(srcDeptName.substring(0, srcDeptName.length() - 1));
        }
        if (!TextUtils.isEmpty(destDeptName)){
            activity_unload_success_desc.setText(destDeptName.substring(0, destDeptName.length() - 1));
        }
        if(carNo!=null&&carNo.length() > 0){
            activity_unload_success_carno.setText(carNo.substring(0, carNo.length() - 1));
        }

        activity_unload_success_checkweight.setText(chargedWeight + "kg");
        activity_unload_success_realweight.setText(weight + "kg");
    }

    @Override
    public String getPageName() {
        return "卸车成功";
    }

    private void initView() {

        activity_unload_success_carcode = (EditText) findViewById(R.id.activity_unload_success_carcode);
        activity_unload_success_src = (EditText) findViewById(R.id.activity_unload_success_src);
        activity_unload_success_desc = (EditText) findViewById(R.id.activity_unload_success_desc);
        activity_unload_success_carno = (EditText) findViewById(R.id.activity_unload_success_carno);
        activity_unload_success_realweight = (EditText) findViewById(R.id.activity_unload_success_realweight);
        activity_unload_success_checkweight = (EditText) findViewById(R.id.activity_unload_success_checkweight);

        activity_unload_success_btn = (Button) findViewById(R.id.activity_unload_success_btn);
        activity_unload_success_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (FastClick.isFastClick()) {
            return;
        }
        switch (v.getId()) {
            case R.id.activity_unload_success_btn:
                finish();
                break;
        }
    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }

}
